package com.budgetme.graphing;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import android.annotation.TargetApi;
import android.graphics.Color;

public class GraphViewSeries {

	/**
	 * graph series style: color and thickness
	 */
	static public class GraphViewSeriesStyle {
		public int color = 0xff0077cc;
		public int thickness = 3;
		
		private int positiveBackgroundColor = Color.argb(255, 0, 100, 0);
		private int negativeBackgroundColor = Color.argb(255, 100, 0, 0);

		public GraphViewSeriesStyle() {
			super();
		}
		
		public GraphViewSeriesStyle(int color, int thickness) {
			super();
			this.color = color;
			this.thickness = thickness;
		}
		
		public void setPosBackgroundColor(int positiveBackgroundColor) {
			this.positiveBackgroundColor = positiveBackgroundColor;
		}
		
		public void setNegBackgroundColor(int negativeBackgroundColor) {
			this.negativeBackgroundColor = negativeBackgroundColor;
		}
		
		public int getPosBackgroundColor() {
			return positiveBackgroundColor;
		}
		
		public int getNegBackgroundColor() {
			return negativeBackgroundColor;
		}

	}

	final String description;
	final GraphViewSeriesStyle style;
	GraphViewData[] data;
	private final List<GraphView> graphViews = new ArrayList<GraphView>();

	public GraphViewSeries(GraphViewData[] data) {
		description = null;
		style = new GraphViewSeriesStyle();
		this.data = data;
		
		Arrays.sort(data);
		
	}

	public GraphViewSeries(String description, GraphViewSeriesStyle style, GraphViewData[] data) {
		super();
		this.description = description;
		if (style == null) {
			style = new GraphViewSeriesStyle();
		}
		this.style = style;
		this.data = data;
		
		Arrays.sort(data);
		
	}

	/**
	 * this graphview will be redrawn if data changes
	 * @param graphView
	 */
	public void addGraphView(GraphView graphView) {
		this.graphViews.add(graphView);
	}

	/**
	 * add one data to current data
	 * @param value the new data to append
	 * @param scrollToEnd true => graphview will scroll to the end (maxX)
	 */
	public void appendData(GraphViewData value, boolean scrollToEnd) {
		GraphViewData[] newValues = new GraphViewData[data.length + 1];
		int offset = data.length;
		System.arraycopy(data, 0, newValues, 0, offset);

		newValues[data.length] = value;
		data = newValues;
		
		Arrays.sort(data);

	}

	public double getMaxX() {
		
		if (data.length == 0) {
			return 0;
		}
		
		double maxX = data[0].valueX;
		
		for(int i = 1; i < data.length; i++) {
			maxX = Math.max(maxX, data[i].valueX);
		}
		
		return maxX;
	}
	
	public double getMinX() {
		
		if (data.length == 0) {
			return 0;
		}
		
		double minX = data[0].valueX;
		
		for(int i = 1; i < data.length; i++) {
			minX = Math.min(minX, data[i].valueX);
		}
		
		return minX;
	}
	
	public double getMaxY() {
		
		if (data.length == 0) {
			return 0;
		}
		
		double maxY = data[0].valueY;
		
		for(int i = 1; i < data.length; i++) {
			maxY = Math.max(maxY, data[i].valueY);
		}
		
		return maxY;
	}
	
	public double getMinY() {
		
		if (data.length == 0) {
			return 0;
		}
		
		double minY = data[0].valueY;
		
		for(int i = 1; i < data.length; i++) {
			minY = Math.min(minY, data[i].valueY);
		}
		
		return minY;
	}
	
	public int size() {
		return data.length;
	}
	
	/**
	 * one data set for a graph series
	 */
	static public class GraphViewData implements Comparable<GraphViewData> {
		protected final double valueX;
		protected final double valueY;
		public GraphViewData(double valueX, double valueY) {
			super();
			this.valueX = valueX;
			this.valueY = valueY;
		}
		@Override
		public int compareTo(GraphViewData another) {
			// TODO Auto-generated method stub
			if (this.valueX < another.valueX) {
				return -1;
			} else if (this.valueX > another.valueX) {
				return 1;
			} else {
				return 0;
			}
		}
	
	}
	
}
